<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>大奔农庄</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/style.css"/>
    <link rel="stylesheet" href="../../css/swiper.min.css">
    <link rel="stylesheet" href="../../font/iconfont.css">

</head>
<body>
	<div class="aui-content aui-margin-b-15" >
		<ul class="aui-list aui-form-list aui-margin-b-15">
	        <li class="aui-list-item">
	            <div class="aui-list-item-inner">
	                <div class="aui-list-item-label"> 收货人 </div>
	                <div class="aui-list-item-input">
	                    <input type="text" placeholder="请输入姓名" id="linkman" name="linkman">
	                </div>
	            </div>
	        </li>
	        <li class="aui-list-item">
	            <div class="aui-list-item-inner">
	                <div class="aui-list-item-label"> 联系电话 </div>
	                <div class="aui-list-item-input">
	                    <input type="number" placeholder="请输入电话" id="contact" name="contact">
	                </div>
	            </div>
	        </li>
	        <li class="aui-list-item aui-list-item-middle">
            	<div class="aui-list-item-inner" id="seleMap">
                	<div class="aui-list-item-label">所在地区</div>
                	<input placeholder="请选择您的收货地址" type="text" id="address" name="address" style="padding-right: 35px;">
              </div>
              <!--
              <div class="aui-list-item-inner aui-list-item-arrow" onclick="selectAdr(this);" id="seleMap">
                	<div class="aui-list-item-label">所在地区</div>
                	<input placeholder="请选择您的收货地址" type="text" id="address" name="address" readonly="true" style="padding-right: 35px;">
                	<div class="aui-list-item-right">
	                    <i class="aui-iconfont aui-icon-location aui-text-success aui-position-location"></i>
	                </div>
              </div>
            -->
            </li>
	        <li class="aui-list-item">
	            <div class="aui-list-item-inner">
	                <div class="aui-list-item-label"> 详细地址 </div>
	                <div class="aui-list-item-input">
	                    <input type="text" placeholder="请输入地址" id="doorplate" name="doorplate">
	                </div>
	            </div>
	        </li>
        </ul>
        <!--
	    <ul class="aui-list border-none">
	        <li class="aui-list-item">
	            <div class="aui-list-item-inner">
	                <div class="aui-list-item-title">设为默认</div>
	                <div class="aui-list-item-right">
	                    <input type="radio" class="aui-radio">
	                </div>
	            </div>
	        </li>
	    </ul>
	    -->
	</div>
	<div class="hong-kuang" onclick="submit();"> <p>保存</p> </div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script>
var db_key = 'user',db_user, myfrom,myParam,adr_obj={};
apiready = function() {
    db_user = $api.getStorage(db_key);//获取用户信息
    myfrom = document.querySelector("#myform");//获取表单
    myParam = api.pageParam;//获取参数

    $linkman = $api.byId("linkman");//收货人
    $contact = $api.byId("contact");//联系电话
    $address = $api.byId("address");//地址
    $doorplate = $api.byId("doorplate");//门牌号

    var tmpdom = document.getElementById('seleMap');
    var adrinput = document.getElementById('address');

    //如果是编辑收货地址，显示地址内容
    if (myParam.myadr.myadr) {
		adr_obj = myParam.myadr;//收货地址信息
        $api.val($linkman, adr_obj.myadr.linkman);
        $api.val($contact, adr_obj.myadr.contact);
        $api.val($address, adr_obj.myadr.address);
        $api.val($doorplate, adr_obj.myadr.doorplate);

        tmpdom.dataset.lon = adr_obj.myadr.lng;
        tmpdom.dataset.lat = adr_obj.myadr.lat;
        tmpdom.dataset.address = adr_obj.myadr.address;
        adrinput.value = adr_obj.myadr.address;
    }
    //监听收货地址改变
    api.addEventListener({
        name: 'adr_save',
    },function(ret, err) {
        var tmpdom = document.getElementById('seleMap');
        var adrinput = document.getElementById('address');
        tmpdom.dataset.lon = ret.value.lon;
        tmpdom.dataset.lat = ret.value.lat;
        tmpdom.dataset.address = ret.value.adrstr;
        adrinput.value = ret.value.adrstr;
    });
}

function submit() {
    if ($linkman.value =="") {
        api.toast({msg: '联系人不能为空！'});
        return false;
    }
    if ($contact.value =="") {
        api.toast({msg: '联系电话不能为空！'});
        return false;
    }
    if ($address.value =="") {
        api.toast({msg: '收货地址不能为空！'});
        return false;
    }if ($doorplate.value =="") {
        api.toast({msg: '门牌号不能为空！'});
        return false;
    }
    api.showProgress();
    var useParam = {
    	rtk : RSapi.myrtk,
        mytoken : db_user.mytoken,
        linkman: $linkman.value,
        contact: $contact.value,
        address: $address.value,
        doorplate: $doorplate.value
    };
    var tmpdom = document.getElementById('seleMap');

    useParam.lng = tmpdom.dataset.lon;
    useParam.lat = tmpdom.dataset.lat;

    if(myParam.myadr.myadr){
    	useParam.id = adr_obj.myadr.adrid;
    	//修改收货地址
    	api.ajax({
	        url: RSapi.base+RSapi.AdrEdit,
	        method: 'post',
	        cache: false,
	        timeout: 30,
	        dataType: 'json',
	        data: {values: useParam},
	    },function(ret, err){
	        if (err) { //alert(err.msg);return false;
	            api.toast({msg:err.msg});
	        }else if (ret.status) {
	            api.alert({msg:ret.info});
				//发送登录成功事件
                api.sendEvent({
                    name: 'adadr_self',
                    extra: {}
                });
                api.closeWin();
	        }else {
	            api.toast({msg:ret.info});
	        }
	        api.hideProgress();
	    });
    }else{
    	//添加收货地址
    	api.ajax({
	        url: RSapi.base+RSapi.AdrAdd,
	        method: 'post',
	        cache: false,
	        timeout: 30,
	        dataType: 'json',
	        data: {values: useParam},
	    },function(ret, err){
	        if (err) { //alert(err.msg);return false;
	            api.toast({msg:err.msg});
	        }else if (ret.status) {
	            api.alert({msg:ret.info});
	            //发送登录成功事件
                api.sendEvent({
                    name: 'adadr_self',
                    extra: {}
                });
                api.closeWin();
	        }else {
	            api.toast({msg:ret.info});
	        }
	        api.hideProgress();
	    });
    }

    return false;
}
function selectAdr(ele) {
    api.openWin({
        name: 'adrmap_win',
        url: '../win/adrmap_win.html',
        pageParam: {myadr : {lon: ele.dataset.lon, lat: ele.dataset.lat, content: ele.dataset.address}}
    });
}
</script>
</html>
